﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Threading;
using Sinacor.Infra.UI.Messaging.SmartClient;
using PSE.Framework.ProviderLoader;
using PSE.Framework.ProviderLoader.Configuration;
using PSE.Framework.UI.Resources;

namespace Sinacor.Infra.UI.Resources.Providers
{
    public class SmartClientResourceProvider : BaseProvider, IResourceProvider
	{
        //private Sinacor.Infra.UI.Messaging.MessageManager _messageManager;
        private WPFMessageManager _messageManager;
        private const string RESOURCEKEY_NOT_FOUND = "The resource key '{0}' could not be found.";
        private const string APPLICATION_UNAVAILABLE = "The resource key '{0}' could not be found, application unavailable.";

        public SmartClientResourceProvider(ProviderConfigurationElement configuration)
            : base(configuration)
        {
            //_messageManager = Sinacor.Infra.UI.Messaging.MessageManager.GetInstance();
            //_messageManager.OnInternalMessageArrival += new Sinacor.Infra.UI.Messaging.MessageManager.InternalMessageArrivalDelegate(_messageManager_OnInternalMessageArrival);
            _messageManager = new WPFMessageManager();
            _messageManager.OnInternalMessageArrival+=new WPFMessageManager.WPFInternalMessageDelegate(_messageManager_OnInternalMessageArrival);
		}

        private void _messageManager_OnInternalMessageArrival(Sinacor.Infra.UI.Messaging.Internal.InternalMessage internalMessage)
		{
			if (internalMessage.MessageId == Sinacor.Infra.UI.Messaging.MessageIds.Application.SNC_APPLICATION_LANGUAGE_CHANGED)
                OnResourceChanged();
		}		

        public object GetResource(object key, params object[] parameters)
        {
            if (key == null)
                throw new ArgumentNullException("key");

            object resource = System.Windows.Application.Current.FindResource(key);

            return string.Format((string)resource, parameters);
        }

        public object TryGetResource(object key, params object[] parameters)
        {
            object resource = null;

            if (key != null && System.Windows.Application.Current != null)
            {
                resource = System.Windows.Application.Current.TryFindResource(key);
            }

            if (resource != null)
            {
                try
                {
                    return string.Format((string)resource, parameters);
                }
                catch (FormatException)
                {
                    return resource;
                }
                catch
                {
                    throw;
                }
            }
            else if (System.Windows.Application.Current != null)
            {
                return string.Format(RESOURCEKEY_NOT_FOUND, key);
            }
            else
            {
                return string.Format(APPLICATION_UNAVAILABLE, key);
            }
        }        

        public bool ResourceExists(object key)
        {
            if (key == null)
                throw new ArgumentNullException("key");

            return (System.Windows.Application.Current.TryFindResource(key) != null);
        }

		public event EventHandler ResourceChanged;
        protected virtual void OnResourceChanged()
		{
			if (ResourceChanged != null)
				ResourceChanged(this, new EventArgs());
		}
    }
}
